// project created on 10/3/2007 at 7:27 PM
using System;
using System.Xml.Xsl;
using System.Xml;
using System.Net.Mail;
using System.Text;
using System.Net.Mime;
using System.Collections.Specialized;
using System.IO;
using FTP;

namespace MailProgramma
{
	class MainClass
	{
		private const string toMailXsl = "./mail-machine/tomail.xsl";
		private const string toTextXsl = "./mail-machine/totextm.xsl";
		private const string toBaggerXsl = "./mail-machine/tobagger.xsl";
		
		public static void Main(string[] args)
		{
			string xmlFileName = args.Length > 0 ? args[0] : "../tekst.xml";
			System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
			
			Console.Write("Type 'yes' als je de mail naar iedereen wilt versturen. Anders wordt alleen een mail naar '" + testEmail + "' gestuurd. [No]");
			string answer = Console.ReadLine();
			bool testing = answer.ToLower() != "yes";
			if(!testing)
				Console.WriteLine("\nMailProgramma - mailt het bestand " + xmlFileName + " naar ALLE mensen uit het bestand ../recipients.txt\n");
			else
				Console.WriteLine("\nMailProgramma - mailt het bestand " + xmlFileName + " naar '" + testEmail + "' om te TESTEN\n");
		
			try
			{
				Mail engine = new Mail(toMailXsl, toTextXsl, toBaggerXsl);
		        XmlDocument input = new XmlDocument();
		        input.Load(xmlFileName);
				
				string recipients;
				if(!testing)
					recipients = File.ReadAllText("../recipients.txt", Encoding.UTF8).Replace("\r\n", "");
				else
					recipients = testEmail;
				
		        engine.CreateHtmls(input.DocumentElement);
		        engine.Send(input.DocumentElement, recipients);
				Console.WriteLine();
				
				string cleanFilename = System.IO.Path.GetFileName(input.DocumentElement.GetAttribute("url"));
				UploadFiles(engine.attachments, cleanFilename, input.DocumentElement.GetAttribute("bijlage"));
				
				string url = ((XmlElement)input.DocumentElement).GetAttribute("url");
				Console.WriteLine("\nDe bulletin staat nu online op '" + url + "' voor als mensen de mail niet goed kunnen lezen");
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
			}
			finally
			{
				Console.WriteLine("Press enter to continue");
				Console.Read();
			}
		}
		
		private static void UploadFiles(StringCollection attachments, string htmlFile, string bijlage)
		{
			Console.WriteLine("Starten van ftp...");
			FTPclient client = new FTPclient(ftpHostname, ftpUsername, ftpPassword);
			
			Console.WriteLine("Ga naar map: " + ftpUploadPath);
			client.CurrentDirectory = ftpUploadPath;
			foreach(string attachment in attachments)
			{
				Console.WriteLine("Upload file: " + Mail.ImagesPath + attachment);
				client.Upload(Mail.ImagesPath + attachment, attachment);
			}
			
			Console.WriteLine("Upload file: ./mail-machine/" + htmlFile);
			client.Upload("./mail-machine/" + htmlFile, htmlFile);
			if(!string.IsNullOrEmpty(bijlage))
			{
				Console.WriteLine("Upload file: " + Mail.BijlagePath + bijlage);
				client.Upload(Mail.BijlagePath + bijlage, bijlage);
			}
		}
		
		private static string ftpHostname
		{
			get	{	
				return System.Configuration.ConfigurationManager.AppSettings["ftp-host"].ToString();
			}
		}
		private static string ftpUsername
		{
			get	{	
				return System.Configuration.ConfigurationManager.AppSettings["ftp-user"].ToString();
			}
		}
		private static string ftpPassword
		{
			get	{	
				return System.Configuration.ConfigurationManager.AppSettings["ftp-password"].ToString();
			}
		}
		private static string ftpUploadPath
		{
			get	{	
				return System.Configuration.ConfigurationManager.AppSettings["ftp-upload-path"].ToString();
			}
		}
		
		private static string testEmail
		{
			get{
				return System.Configuration.ConfigurationManager.AppSettings["test-email"].ToString();
			}
		}
	}
}